using System;
using System.Net;
using System.Reflection;
using InterfaceLiftLib.Service.Interfaces;

namespace InterfaceLiftLib.Service.Implementations
{
    public class MashapeHttpWebRequestBuilder : IHttpWebRequestBuilder
    {
        private const string ServiceTemplate = "https://interfacelift-interfacelift-wallpapers.p.mashape.com/v1/";

        public string MashapeApiKey { get; set; }
 
        public IUrlBuilder UrlBuilder { get; private set; }

        public MashapeHttpWebRequestBuilder(IUrlBuilder urlBuilder, string mashapeApiKey)
        {
            if (urlBuilder == null)
                throw new ArgumentNullException("urlBuilder");
            if (String.IsNullOrEmpty(mashapeApiKey))
                throw new ArgumentNullException("mashapeApiKey");

            UrlBuilder = urlBuilder;
            MashapeApiKey = mashapeApiKey;
        }

        public HttpWebRequest BuildRequest(MethodInfo methodInfo, params object[] parameters)
        {
            if (methodInfo == null)
                throw new ArgumentNullException("methodInfo");

            var wsUrl = UrlBuilder.BuildUrl(ServiceTemplate, methodInfo, parameters);
            var webRequest = (HttpWebRequest) WebRequest.Create(wsUrl);
            webRequest.Method = "GET";
            webRequest.Headers.Add("X-Mashape-Authorization", MashapeApiKey);
            return webRequest;
        }

        
    }
}